﻿// JScript File

function AdsPlayer(xmlSource, itemFasade)              // основной объект, отвечающий за проигрывание тестов        
{
    this.xmlSource = xmlSource;            
    this.xmlSource.async = false;
    if (!isMozilla) this.xmlSource.setProperty("SelectionLanguage", "XPath");   
        
    this.itemFasade = itemFasade;            
    this.items = new Array();
    this.currentItemIndex = 0;
    this.markedOnly = false;            
    this.finished = false;    
    this.testTitle = '';//this.xmlSource.getElementsByTagName("assessmentTest")[0].attributes.getNamedItem("title").text;
    this.report;
    this.OnLoaded;
    this.OnPassed;
 
    this.Next = function()
    {                   
        UnSetUnBlur();
        var index = this.currentItemIndex;
        index++;                
        if (index < this.items.length)
        {
            if (this.markedOnly)
            {
                while(!isMozilla ? (this.items[index].xml.attributes.getNamedItem("marked").text == "false") : (this.items[index].xml.attributes.getNamedItem("marked").textContent == "false"))
                {
                    index++;
                    if (index == this.items.length)                       
                        break;
                }
            }
        }
        if (index < this.items.length)
            this.ShowItem(index);
        else
        {
            this.SetBCQNN("true");
            this.ShowTest();
            this.SetBCQNN("false");
        }
    }
    
    this.Previous = function()
    {                
        UnSetUnBlur();
        var index = this.currentItemIndex;
        index--;
        if (index >= 0)
        {
            if (this.markedOnly)
            {
                while(!isMozilla ? (this.items[index].xml.attributes.getNamedItem("marked").text == "false") : (this.items[index].xml.attributes.getNamedItem("marked").textContent == "false"))
                {
                    index--;
                    if (index < 0)                       
                        break;
                }
            }
        }
        if (index >= 0)
            this.ShowItem(index);
        else
        {
            this.SetBCQNN("true");
            this.ShowTest();
            this.SetBCQNN("false");
        }    
    }
    
    // внимание! this.currentItemIndex <> index;               
    this.ShowItem = function(index)
    {        
        this.items[this.currentItemIndex].ClearCurrent();            
        this.currentItemIndex = index;                
        var html = "<h3>" + this.testTitle + "</h3>";
        html += this.items[this.currentItemIndex].Render();
        this.itemFasade.innerHTML = html;
    }                       
    
    this.ShowTest = function()
    {   
        //alert(document.body.innerHTML);
        this.markedOnly = false;
        this.itemFasade.innerHTML = TransformNode(this.xmlSource, "xslt/Test.xsl.aspx");        
        
        var currentTr = document.getElementById('currentQuestionTr'); //currentQuestionTr sets up in Test.xsl template
        if (currentTr != null && currentTr != undefined)
        {
            var links = currentTr.cells[0].getElementsByTagName("a");
            links[0].scrollIntoView();
        }
    }
    
    this.SetBCQNN = function(value)     // Button CurrentQuestion Not Need
    {   // value - string!
          var test = this.xmlSource.getElementsByTagName("assessmentTest")[0];
          var BCQNN = test.attributes.getNamedItem("BCQNN");
          if (BCQNN == null)
          {
              BCQNN = this.xmlSource.createAttribute("BCQNN");                              //  Button Current Question Not Needed              
              test.attributes.setNamedItem(BCQNN);
          }
          
          if (!isMozilla)           
            BCQNN.text = value;
          else
            BCQNN.textContent = value;
    }
    
    this.Finish = function(needToAsk)
    {
        var ok = false;    
        if (needToAsk)
        {
            if (Confirm(AdsPlayer_FinishTestConfirmText) == true)
                ok = true;
        }
        else ok = true;        
        if (ok)
        {
            this.finished = true;            
            TurnOffStrictControl();  // чтоб больше не было сообщений о том, что нельзя переключать фокус ввода
            
            if (this.OnPassed != null)
                this.OnPassed();

            this.xmlSource.getElementsByTagName("assessmentTest")[0].getElementsByTagName("timeLimits")[0].attributes.getNamedItem("maxTime").value = timerPointer.time;

            timerPointer.Stop();    // кольцевая ссылка...  подругому никак               
            autoSaveTimerPointer.Stop();                     
            timerPointer.fasade.innerHTML = "";
            
            this.report = new ReportManager(this.xmlSource, this.itemFasade);
            this.report.Report();            
        }       
    }        
    
    this.Interact = function(event)
    {
        this.items[this.currentItemIndex].Interact(event);
    }
    
    this.MarkCurrentItem = function(event)
    {    
        var aEvent = event ? event : window.event;        
        var opt = aEvent.srcElement ? aEvent.srcElement : aEvent.target;        
        if (opt.checked)
            this.items[this.currentItemIndex].Mark();
        else
            this.items[this.currentItemIndex].UnMark();
    }
    
    this.MarkItem = function(index, event)
    {    
        var aEvent = event ? event : window.event;        
        var opt = aEvent.srcElement ? aEvent.srcElement : aEvent.target;
        if (opt.checked)
            this.items[index].Mark();
        else
            this.items[index].UnMark();
    }
    
    this.MarkedItemsOnly = function()
    {                   
        var index = 0                            
        while(!isMozilla ? (this.items[index].xml.attributes.getNamedItem("marked").text == "false") : (this.items[index].xml.attributes.getNamedItem("marked").textContent == "false"))
        {        
            index++;
            if (index == this.items.length)                       
            {
                ShowMessage(AdsPlayer_MarkedQuestionsNotFound);
                return;
            }    
        }        
        
        this.markedOnly = true;          
        this.ShowItem(index);            
    }
    
    this.Init = function()
    {
        var test = this.xmlSource.getElementsByTagName("assessmentTest")[0];
        var testPartsXml = test.getElementsByTagName("testPart");
                    
        for (var i = 0; i < testPartsXml.length; i++)
        {
            var sectionsXml = GetDirectChildNodes(testPartsXml[i], "assessmentSection");                        
            for (var n = 0; n < sectionsXml.length; n++)
                AddSectionsItemToArray(this.items, sectionsXml[n]);
        }            

        var index = 0;
        for (var i = 0; i < this.items.length; i++)
        {        
            this.items[i].SetItemCount(this.items.length); 
            if (!isMozilla ? (this.items[i].xml.attributes.getNamedItem("current").text == "true") : (this.items[i].xml.attributes.getNamedItem("current").textContent == "true"))
                index = i;
        }                        
        this.SetBCQNN("false");
        this.ShowItem(index);                
        
        if (this.OnLoaded != null)
            this.OnLoaded();
    }            
}